package lab03.test09;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {

        //List 保存内容
        //单步调试

        List<Book> books = new ArrayList<Book>();
        String path = "D:\\dev\\lab03\\book1.txt";
        String path1 = "D:\\dev\\lab03\\book2.txt";

        //读文件


        BufferedReader reader = null;

        //读出文件内的内容，要放在合适的类型的集合里，判断每一字段内的内容去分割
        //一行一行去读取

        try {
            reader = new BufferedReader(new FileReader(path));

            String line = null;

            while ((line = reader.readLine())!=null){
                String[] tokens = line.split("\t");
                Book book = new Book();

//                String string = "1001";
//                book.bookNo = Integer.parseInt(string);
                System.out.println(tokens[0].substring(tokens[0].length()-4,tokens[0].length()));
                book.bookNo = Integer.parseInt(tokens[0].substring(tokens[0].length()-4,tokens[0].length()));
                book.bookName = tokens[1];
                book.version = tokens[2];
                book.price = Double.parseDouble(tokens[3]);
                book.sales = Double.parseDouble(tokens[4]);

                books.add(book);
            }
            int i = 1;
            for (Book b : books){
                if (i == 3){
                    b.price = b.price +20;
                }
                if (i == 4){
                    b.price = b.price +30;
                }
                i++;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new FileWriter(path1));

            for (Book b : books){
                writer.write(b.bookNo + "\t"+b.bookName+
                        "\t"+b.version+"\t"+b.price+"\t"+
                        b.sales);

                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            reader.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}